This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 07-11 05:25:08] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 07-11 05:25:08] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 07-11 05:25:08] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 07-11 05:25:08] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 07-11 05:25:08] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 07-11 05:25:08] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.456306, 'x2': 0.449093, 'x3': 0.848279, 'x4': 0.408463, 'x5': 0.131662, 'x6': 0.535017}.
[INFO 07-11 05:25:08] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.629903, 0.1), 'l2norm': (1.219165, 0.1)}.
[INFO 07-11 05:25:08] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.394698, 'x2': 0.553497, 'x3': 0.696356, 'x4': 0.05392, 'x5': 0.821321, 'x6': 0.263603}.
[INFO 07-11 05:25:08] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.227921, 0.1), 'l2norm': (1.40758, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.46493, 'x2': 0.042577, 'x3': 0.146211, 'x4': 0.974435, 'x5': 0.673758, 'x6': 0.047545}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.124355, 0.1), 'l2norm': (1.261322, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.030657, 'x2': 0.895044, 'x3': 0.418681, 'x4': 0.742791, 'x5': 0.012055, 'x6': 0.253158}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.081895, 0.1), 'l2norm': (1.306623, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.651226, 'x2': 0.894723, 'x3': 0.457437, 'x4': 0.795646, 'x5': 0.09618, 'x6': 0.327471}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.318388, 0.1), 'l2norm': (1.497753, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.512542, 'x2': 0.067704, 'x3': 0.016925, 'x4': 0.038909, 'x5': 0.941869, 'x6': 0.824653}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.045432, 0.1), 'l2norm': (1.45019, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.316759, 'x2': 0.074311, 'x3': 0.232216, 'x4': 0.149163, 'x5': 0.11588, 'x6': 0.237637}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.330783, 0.1), 'l2norm': (0.501911, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.192864, 'x2': 0.831832, 'x3': 0.215698, 'x4': 0.319791, 'x5': 0.168492, 'x6': 0.625296}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.464374, 0.1), 'l2norm': (1.139439, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.023483, 'x2': 0.459241, 'x3': 0.717233, 'x4': 0.441897, 'x5': 0.336513, 'x6': 0.376035}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.675634, 0.1), 'l2norm': (0.9178, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.513012, 'x2': 0.504004, 'x3': 0.315785, 'x4': 0.700443, 'x5': 0.742958, 'x6': 0.251481}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.272066, 0.1), 'l2norm': (1.488105, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.347105, 'x2': 0.0163, 'x3': 0.136654, 'x4': 0.826229, 'x5': 0.123546, 'x6': 0.417755}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.056079, 0.1), 'l2norm': (0.9596, 0.1)}.
[INFO 07-11 05:25:09] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.779251, 'x2': 0.853795, 'x3': 0.843395, 'x4': 0.725278, 'x5': 0.064474, 'x6': 0.854198}.
[INFO 07-11 05:25:09] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (0.038798, 0.1), 'l2norm': (1.837943, 0.1)}.
[INFO 07-11 05:25:22] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.19835, 'x2': 0.448317, 'x3': 0.733207, 'x4': 0.385672, 'x5': 0.238854, 'x6': 0.453475}.
[INFO 07-11 05:25:22] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-1.071177, 0.1), 'l2norm': (1.057469, 0.1)}.
[INFO 07-11 05:25:23] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.198573, 'x2': 0.467555, 'x3': 0.685248, 'x4': 0.305954, 'x5': 0.21703, 'x6': 0.48954}.
[INFO 07-11 05:25:23] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-1.279914, 0.1), 'l2norm': (0.936567, 0.1)}.
[INFO 07-11 05:25:30] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.168026, 'x2': 0.431881, 'x3': 0.715317, 'x4': 0.248945, 'x5': 0.186848, 'x6': 0.537673}.
[INFO 07-11 05:25:30] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.403652, 0.1), 'l2norm': (1.043908, 0.1)}.
[INFO 07-11 05:25:36] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.177679, 'x2': 0.493248, 'x3': 0.701864, 'x4': 0.186071, 'x5': 0.175311, 'x6': 0.498274}.
[INFO 07-11 05:25:36] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.202704, 0.1), 'l2norm': (1.110533, 0.1)}.
[INFO 07-11 05:25:41] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.17825, 'x2': 0.416941, 'x3': 0.652328, 'x4': 0.259693, 'x5': 0.218716, 'x6': 0.605207}.
[INFO 07-11 05:25:41] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.015828, 0.1), 'l2norm': (0.944897, 0.1)}.
[INFO 07-11 05:25:45] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.172098, 'x2': 0.396078, 'x3': 0.615809, 'x4': 0.26643, 'x5': 0.239731, 'x6': 0.649849}.
[INFO 07-11 05:25:45] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.301553, 0.1), 'l2norm': (0.90546, 0.1)}.
[INFO 07-11 05:25:47] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.166411, 'x2': 0.370253, 'x3': 0.590699, 'x4': 0.251228, 'x5': 0.262571, 'x6': 0.702855}.
[INFO 07-11 05:25:47] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.582999, 0.1), 'l2norm': (0.92753, 0.1)}.
[INFO 07-11 05:25:48] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.151714, 'x2': 0.363212, 'x3': 0.578612, 'x4': 0.244206, 'x5': 0.259104, 'x6': 0.780882}.
[INFO 07-11 05:25:48] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.422303, 0.1), 'l2norm': (1.197981, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 07-11 05:25:48] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 59.74 seconds.